﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tui;
using System.Data.SqlServerCe;
using System.IO;

namespace TuiTest
{
    static class Tui
    {

        public static void Test()
        {
            var cactusCustomer = Customer.Read("CACTU");
            var allCustomers = Customer.Read().ToArray();

            var custsA = new[] { "OTTIK", "FISSA", "PARIS", "WOLZA", "AROUT", "HUNGO" };

            var custsB = custsA.Join<Customer, string>("Customers_PK", q => q);
            var custsC = Customer.Read(custsA, "Customers_PK", q => q);
            var custsD = Customer.Read(custsA, q => q);
            IEnumerable<TuiTest.Customer> custsE = Customer.Read().IndexBetween("FISSA", "PARIS");
            IEnumerable<TuiTest.Customer> custsF = Customer.Read().IndexGTE("PARIS");


            //now lazyly execute the queries.
            cactusCustomer = cactusCustomer.ToArray();
            allCustomers = allCustomers.ToArray();
            custsA = custsA.ToArray();
            custsB = custsB.ToArray();
            custsC = custsC.ToArray();
            custsD = custsD.ToArray();
            custsE = custsE.ToArray();
            custsF = custsF.ToArray();



            var orders = Order.Read(custsA, "Customer ID", c => c);


            var cNew = new Customer() { CustomerId = "NEWON", CompanyName = "New One", ContactTitle = "Ms", ContactName = "Smith" };
            cNew.Save();//first time inserts a new row, next time updates it.

            var theNewOne = Customer.Read().IndexMatches("NEWON").First();
            theNewOne.ContactName = "Smyth";
            theNewOne.Save();


            theNewOne.Delete();



        }
    }






    public abstract class TestBaseBusinessObject<T> : BusinessObject<T> where T : BusinessObject<T>, new()
    {
        private static SqlCeConnection _cnn;
        protected static SqlCeConnection ConnectionGet()
        {
            if (_cnn == null)
            {
                string cs = "data source='" + SdfFile.GetFile() + "'";
                (_cnn = new SqlCeConnection(cs)).Open();
            }
            return _cnn;
        }
        protected static void ConnectionRelease(SqlCeConnection cnn) { }
    }

    [TableName("Customers")]
    class Customer : TestBaseBusinessObject<Customer>
    {
        public string CustomerId { get; set; }
        public string CompanyName { get; set; }
        public string ContactName { get; set; }
        public string ContactTitle { get; set; }

        public override string ToString() { return CustomerId + " - " + CompanyName + " " + ContactTitle + " " + ContactName; }
    }

    [TableName("Orders")]
    class Order : TestBaseBusinessObject<Order>
    {
        public string OrderId { get; set; }
        public string CustomerId { get; set; }
        public string EmployeeId { get; set; }

        public override string ToString() { return OrderId.ToString() + " - " + CustomerId + " - " + EmployeeId; }// + " - " + CompanyName + " " + ContactTitle + " " + ContactName; }
    }


}
